home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / less-252 / ttyin.c < prev    next >
C/C++ Source or Header  |  1995-05-11  |  3KB  |  111 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Routines dealing with getting input from the keyboard (i.e. from the user).
  30.  */
  31.  
  32. #include "less.h"
  33. #if HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36.  
  37. static int tty;
  38.  
  39. /*
  40.  * Open keyboard for input.
  41.  */
  42.     public void
  43. open_getchr()
  44. {
  45. #if MSOFTC
  46.     extern int fd0;
  47.     /*
  48.      * Open a new handle to CON: in binary mode 
  49.      * for unbuffered keyboard read.
  50.      */
  51.      fd0 = dup(0);
  52.      close(0);
  53.      tty = open("CON", O_RDONLY|O_BINARY);
  54. #else
  55.     /*
  56.      * Try /dev/tty.
  57.      * If that doesn't work, use file descriptor 2,
  58.      * which in Unix is usually attached to the screen,
  59.      * but also usually lets you read from the keyboard.
  60.      */
  61.     tty = open("/dev/tty", 0);
  62.     if (tty < 0)
  63.         tty = 2;
  64. #endif
  65. }
  66.  
  67. /*
  68.  * Get a character from the keyboard.
  69.  */
  70.     public int
  71. getchr()
  72. {
  73.     char c;
  74.     int result;
  75.  
  76.     do
  77.     {
  78. #if MSOFTC
  79.         /*
  80.          * In raw read, we don't see ^C so look here for it.
  81.          */
  82.         flush();
  83.         c = getch();
  84.         result = 1;
  85.         if (c == '\003')
  86.             return (READ_INTR);
  87. #else
  88.         result = iread(tty, &c, sizeof(char));
  89.         if (result == READ_INTR)
  90.             return (READ_INTR);
  91.         if (result < 0)
  92.         {
  93.             /*
  94.              * Don't call error() here,
  95.              * because error calls getchr!
  96.              */
  97.             quit(1);
  98.         }
  99. #endif
  100.         /*
  101.          * Various parts of the program cannot handle
  102.          * an input character of '\0'.
  103.          * If a '\0' was actually typed, convert it to '\340' here.
  104.          */
  105.         if (c == '\0')
  106.             c = '\340';
  107.     } while (result != 1);
  108.  
  109.     return (c & 0377);
  110. }
  111.